<?php
/*
 * @Author: your name
 * @Date: 2020-10-14 16:54:12
 * @LastEditTime: 2020-12-28 10:08:17
 * @LastEditors: Please set LastEditors
 * @Description: 防火墙通信规则
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/FirewallSafeRuleService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter;

use app\adminapiv3\model\NetfilterFirewallRuleModel;
use app\adminapiv3\service\BaseService;
use app\common\definition\HostDefinition;
use app\common\exception\CustomException;
use app\common\service\module_manage\netfilter\NetfilterSafeRule;
use app\common\definition\netfilter\FirewallRuleDefinition;
use think\Db;
use think\Exception;

class FirewallSafeRuleService extends BaseService
{
        /**
     * @name: 通信规则列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 16:17:25
     */
    public function safeRuleList ($host_id)
    {
        $return = Db::name('NetfilterSafeRule')->where('host_id', $host_id)->paginate()->toArray();

        return $return;
    }

    /**
     * @name: 默认规则 开启/关闭
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 16:17:56
     */
    public function safeRuleOn ($host_id, $rule_id)
    {
        $host_id_a = $this->getHost('id');
        $NetfilterSafeRule = new NetfilterSafeRule();

        $rule_data = Db::name('netfilter_safe_rule')->field('id,host_id,content')->where('host_id', $host_id)->whereIn('id', $rule_id)->where('status', FirewallRuleDefinition::RULE_DISABLE)->select();

        //添加操作日志
        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '启用默认规则',
            'new_info' => '主机:' . $host_name_s . '规则内容:' . implode(',', array_column($rule_data, 'content'))
        );

        if (empty($rule_data))
        {
            throw new CustomException('未找到默认规则', 422);
        }

        if (count(explode(',', $rule_id)) != count($rule_data))
        {
            throw new CustomException('参数不匹配', 422);
        }
        
        $save_data = array_map(function($v) {
            $return = [
                'host_id' => $v['host_id'],
                'rule_id' => $v['id'],
                'source' => FirewallRuleDefinition::CONNECT_RULE_SOURCE,
                'content' => $v['content'],
                'rank' => 0,
                'region' => FirewallRuleDefinition::CONNECT_NORMAL_RULE_REGION
            ];
            return $return;
        }, $rule_data);
        
        Db::startTrans();
        try {
            $NetfilterSafeRule->trunOnOff($host_id_a, $rule_id, FirewallRuleDefinition::RULE_ENABLE);
            $NetfilterFirewallRuleModel = new NetfilterFirewallRuleModel();
            $NetfilterFirewallRuleModel->saveAll($save_data);
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            throw new CustomException('插入数据失败:' . $e->getMessage(), 302);
        }
        
        //将linux 规则写入到文件供规则程序调用
        $host_a = $this->getHost();
        foreach ($host_a as $host_id => $host_data)
        {
            if($host_data['os'] == HostDefinition::LINUX_OPERATION_SYSTEM)
            {
                $SortRuleService = new SortRuleService();
                $SortRuleService->pushContentToFile($host_id);
            }
        }

        $return['rule_id'] = $rule_id;

        return $return;
    }
    
    /**
     * @name: 默认规则 开启/关闭
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 16:17:56
     */
    public function safeRuleOff ($host_id, $rule_id)
    {
        $NetfilterSafeRule = new NetfilterSafeRule();
        
        $rule_data = Db::name('netfilter_safe_rule')->field('id,host_id,content')->where('host_id', $host_id)->whereIn('id', $rule_id)->where('status', 1)->select();

        //添加操作日志
        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '禁用默认规则',
            'old_info' => '主机:' . $host_name_s . '规则内容:' . implode(',', array_column($rule_data, 'content'))
        );

        if (empty($rule_data))
        {
            throw new CustomException('未找到默认规则', 422);
        }

        if (count(explode(',', $rule_id)) != count($rule_data))
        {
            throw new CustomException('参数不匹配', 422);
        }

        Db::startTrans();
        try {
            $NetfilterSafeRule->trunOnOff($host_id, $rule_id, FirewallRuleDefinition::RULE_DISABLE);
            Db::name('netfilter_firewall_rule')->where('host_id', $host_id)->where('source', FirewallRuleDefinition::CONNECT_RULE_SOURCE)->whereIn('rule_id', $rule_id)->delete();
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            throw new CustomException('删除数据失败', 302);
        }

        $return['rule_id'] = $rule_id;

        return $return;
    }
}